/**
 * Tools System Tests
 * 
 * Tests for the tools system including built-in tools and tool registry
 */

import { describe, test, expect } from 'bun:test';
import { toolRegistry, createTextResult } from '@/tools/tool-factory';
import { builtinTools } from '@/tools/builtin';
import { calculatorTool } from '@/tools/builtin/calculator';

describe('Tool Registry', () => {
  test('should register built-in tools', () => {
    toolRegistry.registerTools(builtinTools);
    
    const allTools = toolRegistry.getAllTools();
    expect(allTools.length).toBeGreaterThan(0);
  });

  test('should get tool by name', () => {
    toolRegistry.registerTool(calculatorTool);
    
    const tool = toolRegistry.getTool('calculator');
    expect(tool).toBeDefined();
    expect(tool?.name).toBe('calculator');
  });

  test('should create SDK MCP server', () => {
    toolRegistry.registerTools(builtinTools);
    
    const server = toolRegistry.createServer('test-server', ['calculator']);
    expect(server).toBeDefined();
  });
});

describe('Calculator Tool', () => {
  test('should perform basic arithmetic', async () => {
    const result = await calculatorTool.handler({ expression: '2 + 2' });
    
    expect(result.content).toBeDefined();
    expect(result.content[0].text).toContain('4');
  });

  test('should handle multiplication', async () => {
    const result = await calculatorTool.handler({ expression: '10 * 5' });
    
    expect(result.content[0].text).toContain('50');
  });

  test('should reject invalid expressions', async () => {
    const result = await calculatorTool.handler({ expression: 'alert("xss")' });
    
    expect(result.content[0].text).toContain('Error');
  });
});

describe('Helper Functions', () => {
  test('should create text result', () => {
    const result = createTextResult('Hello, world!');
    
    expect(result.content).toBeDefined();
    expect(result.content[0].type).toBe('text');
    expect(result.content[0].text).toBe('Hello, world!');
  });
});

